/*
 * WhatsThisManager.java  0.4.0 / Oct 27, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

/**
 * What's This Manager.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public final class WhatsThisManager {

// Private Static Fields. ------------------------------------------------------

  private static boolean enabled;

// Static Initialization. ------------------------------------------------------

  static {
    enabled = true;
    Swing.initialize();
  }

// Private Constructor. --------------------------------------------------------

 /**
  * Class has no instances.
  */
  private WhatsThisManager() {
    // empty constructor body
  }

// Public Static Methods. ------------------------------------------------------

  /**
   * Returns <tt>true</tt> if the what's this help is enabled.
   *
   * @return <tt>true</tt> if the what's this help is enabled
   */
  public static boolean isWhatsThisEnabled() {
    return enabled;
  }

  /**
   * Enables/disables what's this help.
   *
   * @param enabled <tt>true</tt> if enabled
   */
  public static void setWhatsThisEnabled(boolean enabled) {
    if (WhatsThisManager.enabled != enabled) {
      synchronized (Swing.CLASS_LOCK) {
        Swing.disposedCheck();
        _setEnabled(enabled);
      }
      WhatsThisManager.enabled = enabled;
    }
  }

  /**
   * Shows <tt>what's this</tt> help location.
   *
   * @param text the what's this help
   * @param x the x-coordinate of the <tt>what's this</tt> help
   * @param y int y-coordinate of the <tt>what's this</tt> help
   */
  public static void showWhatsThis(String text, int x, int y) {
    showWhatsThis(text, x, y, null);
  }

  /**
   * Displays <tt>what's this</tt> help at specified location.
   *
   * @param text the what's this help
   * @param x the x-coordinate of the <tt>what's this</tt> help
   * @param y int y-coordinate of the <tt>what's this</tt> help
   * @param component the component for which <tt>what's this</tt> help is
   *                  provided
   */
  public static void showWhatsThis(String text, int x, int y,
                                   Component component) {
    if (text != null) {
      synchronized (Swing.CLASS_LOCK) {
        Swing.disposedCheck();
        if (component == null) {
          _showText(text, x, y, 0);
        } else {
          _showText(text, x, y, component._delegate);
        }
      }
    }
  }

  /**
   * Hides <tt>what's this</tt> help.
   */
  public static void hideWhatsThis() {
    synchronized (Swing.CLASS_LOCK) {
      Swing.disposedCheck();
      _hideText();
    }
  }

  /**
   * Returns <tt>true</tt> if the <tt>what's this</tt> mode is active.
   *
   * @return <tt>true</tt> if the <tt>what's this</tt> mode is active
   */
  public static boolean isInWhatsThisMode() {
    synchronized (Swing.CLASS_LOCK) {
      Swing.disposedCheck();
      return _isInWhatsThisMode();
    }
  }

  /**
   * Enters/leaves <tt>what's this</tt> mode.
   */
  public static void enterWhatsThisMode() {
    synchronized (Swing.CLASS_LOCK) {
      Swing.disposedCheck();
      _enterWhatsThisMode();
    }
  }

  /**
   * Enters/leaves <tt>what's this</tt> mode.
   */
  public static void exitWhatsThisMode() {
    synchronized (Swing.CLASS_LOCK) {
      Swing.disposedCheck();
      _exitWhatsThisMode();
    }
  }

// Package-Private Native Method. ----------------------------------------------

  static native void _setEnabled(boolean enabled);

  static native void _showText(String text, int x, int y,
                               long componentDelegate);

  static native void _hideText();

  static native boolean _isInWhatsThisMode();

  static native  void _enterWhatsThisMode();

  static native void _exitWhatsThisMode();

}
